package com.lucenly.spell.utils

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import java.util.*

/**
 * Created by Yang on 2016/3/10.
 *
 * 应用程序Activity管理类：用于Activity管理和应用程序退出
 */
class AppManager private constructor() {
    /**
     * 添加Activity到堆栈,一般在baseaty里面调用这个方法
     */
    fun addActivity(activity: Activity?) {
        if (activityStack == null) {
            activityStack = Stack()
        }
        activityStack!!.add(activity)
        System.gc()
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivity(): Activity? {
        return if (activityStack != null && activityStack!!.size > 0) {
            activityStack!!.lastElement()
        } else {
            null
        }
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    fun finishActivity() {
        if (activityStack != null) {
            val activity = activityStack!!.lastElement()
            finishActivity(activity)
        }
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity?) {
        var activity = activity
        if (activity != null && activityStack != null) {
            activityStack!!.remove(activity)
            activity.finish()
            activity = null
        }
    }

    /**
     * 结束指定类名重复的Activity
     */
    fun finishActivity(cls: Class<*>) {
        if (activityStack != null) {
            for (i in activityStack!!.indices) {
                val activity = activityStack!![i]
                if (activityStack!![i]!!.javaClass == cls) {
                    finishActivity(activity)
                    break
                }
            }
        }
    }

    /**
     * 移除指定类名重复的Activity
     *
     * @param cls
     */
    fun finishActivitys(cls: Class<*>) {
        if (activityStack != null) {
            for (i in activityStack!!.indices) {
                val activity = activityStack!![i]
                if (activityStack!![i]!!.javaClass == cls) {
                    finishActivity(activity)
                }
            }
        }
    }

    /**
     * 移除指定类名重复的Activity  当前只有一个的话就不移除
     */
    fun finishRepeatActivity(cls: Class<*>): Int {
        if (activityStack != null) {
            for (i in activityStack!!.indices) {
                val activity = activityStack!![i]
                if (activityStack!![i]!!.javaClass == cls && activity !== activityStack!!.lastElement()) {
                    finishActivity(activity)
                    return i
                }
            }
        }
        return -1
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size = activityStack!!.size
        while (i < size) {
            if (null != activityStack!![i]) {
                activityStack!![i]!!.finish()
            }
            i++
        }
        activityStack!!.clear()
    }

    /**
     * 判断某个Activity在不在堆栈中
     */
    fun isCreateIt(cls: Class<*>): Boolean {
        if (activityStack != null) {
            for (i in activityStack!!.indices) {
                if (activityStack!![i]!!.javaClass == cls) {
                    return true
                }
            }
        }
        return false
    }

    /**
     * 退出应用程序
     */
    fun AppExit(context: Context) {
        try {
            finishAllActivity()
            val activityMgr = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            activityMgr.restartPackage(context.packageName)
            System.exit(0)
        } catch (e: Exception) {
        }
    }

    companion object {
        private var activityStack: Stack<Activity?>? = null
        private var instance: AppManager? = null

        /**
         * 单一实例
         */
        val appManager: AppManager?
            get() {
                if (instance == null) {
                    instance = AppManager()
                }
                return instance
            }
    }
}